﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Data.Sql;
namespace HZ.Supervise.Web.Common
{
      public class TableGetHtml
    {
          /// <summary>
          /// 根据table拼sql 
          /// </summary>
          /// <param name="dt">传入table </param>
          /// <param name="nsort">0 类别采购对比，1类别入库对比</param>
          /// <returns></returns>
          static public string GetTableHtml(DataTable dt,int nsort)
          {
              StringBuilder stb = new StringBuilder();
              stb.Append("<table class=\"mainlist\">");
              stb.Append("<tr><td colspan=\"2\" rowspan=\"2\"> &nbsp;</td> <td colspan=\"2\">1月</td> </tr>");
              stb.Append("<tr><td>入库数量</td><td>入库金额</td></tr>");
              stb.Append("<tr><td rowspan=\"2\">默认类别</td><td> 基本</td><td>10</td><td>1000.00</td></tr>");
              stb.Append("<tr><td>非基本</td><td>20</td><td> 20000.00</td></tr>");
              stb.Append("</table>");
              return stb.ToString();
          }
          public DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
          {
              DataSet ds = new DataSet();
              DataTable dt = new DataTable(TableName);
              dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);

              object LastValue = null;
              foreach (DataRow dr in SourceTable.Select("", FieldName))
              {
                  if (LastValue == null || !(ColumnEqual(LastValue, dr[FieldName])))
                  {
                      LastValue = dr[FieldName];
                      dt.Rows.Add(new object[] { LastValue });
                  }
              }
              if (ds != null)
                  ds.Tables.Add(dt);
              return dt;
          }
          private bool ColumnEqual(object A, object B)
          {

              if (A == DBNull.Value && B == DBNull.Value) //  both are DBNull.Value
                  return true;
              if (A == DBNull.Value || B == DBNull.Value) //  only one is DBNull.Value
                  return false;
              return (A.Equals(B));  // value type standard comparison
          }
    }
}
